﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfDemo.Models;
using WpfDemo.Models.Sys;

namespace WpfDemo.Common
{
    public class MenuHelper
    {
        public static void FillMenus(ObservableCollection<MenuItemModel> menus,MenuItemModel parent, List<ModuleEntity>? origMenus, bool expandedNode = true)
        {
            var sub = origMenus.Where(m => m.F_ParentId == (parent == null ? "0" : parent.F_Id))
                .OrderBy(o => o.F_Sort)
                .ToList();
            var menuLst = new ObservableCollection<ItemCheck>();
            if (sub.Count() > 0)
            {
                foreach (ModuleEntity item in sub)
                {
                    menuLst = new ObservableCollection<ItemCheck>();
                    if (item.menus!=null && item.menus.Count() > 0)
                    {
                        item.menus.ForEach(m => menuLst.Add(m));
                    }
                    MenuItemModel model = new MenuItemModel
                    {
                        F_Id = item.F_Id,
                        F_Name = item.F_Name,
                        F_Icon = item.F_Icon,
                        F_TargetView = item.F_TargetView,
                        F_Sort = item.F_Sort,
                        F_ParentId = (parent == null ? "0" : parent.F_Id),
                        F_IsExpanded = expandedNode,
                        F_Remark = item.F_Remark,
                        F_Parent = parent,
                        Buttons = menuLst,
                    };
                    menus.Add(model);

                    FillMenus(model.Children, model, origMenus);
                }

                if (menus.Count > 0)
                {
                    menus[menus.Count - 1].IsLastChild = true;
                }
            }
        }
    }
}
